<?php

function smarty_function_innopager($params, &$smarty) {
    // START INIT
    
    $startnum  = FormUtil::getPassedValue ('startnum', 0, 'REQUEST');


    foreach($params as $key=>$value)    {
        $tmps[strtolower($key)] = $value;
        $tmp = strtolower($key);
        if (!(${$tmp} = $value))    {
            ${$tmp} = '';
        }
    }
    settype($shift, 'integer');

    // If startmodule, check startargs for posvar
    if (empty($Request['module'])) {
    	$funcargs = explode(',', pnConfigGetVar('startargs'));
    	$arguments = array();
    	foreach ($funcargs as $funcarg) {
            if (!empty($funcarg)) {
                $argument=explode('=', $funcarg);
                if ($argument[0]==$posvar)
                    $Request[$posvar] = $argument[1];
            }
    	}
    }

    // START data check
    $minVars = array('limit');
    foreach($minVars as $tmp)  {
        if (empty($params[$tmp]))    {
            $smarty->trigger_error('plugin "pager": missing or empty parameter: "'.$tmp.'"');
        }
    }
    // END data check

    if ($txt_pos == 'middle')    {
        $txt_pos = 'side';
    }
    
    if (!in_array($txt_pos, array('side', 'top', 'bottom'))) {
            $smarty->trigger_error('plugin "pager": bad value for : "txt_pos"');
    }
		
  $ctrl  = FormUtil::getPassedValue ('ctrl', null, 'REQUEST');	
  $type  = FormUtil::getPassedValue ('type', null, 'REQUEST');	
  $func  = FormUtil::getPassedValue ('func', null, 'REQUEST');	
	
	$type_string    = isset($type)    ? "&type=".$type    : "&type=user";
	$ctrl_string    = isset($ctrl)    ? "&ctrl=".$ctrl    : "&ctrl=home";
	$func_string    = isset($func)    ? "&func=".$func    : "&func=page";
	
  $range = 7;
  $page = $startnum/$limit;

  $totalpage = ceil($rowcount/$limit);
  $goto = ($page - 1) * $limit;
  $start = $page - $range;
  $end = $page + $range;
	
	
	//Remove startnum argument
	$request_uri_explode = explode("&", $tmps['request_uri']);
	$new_request_uri_explode = array();
	$element = 0;
	for($i=0; $i<count($request_uri_explode); $i++){
		if(strpos($request_uri_explode[$i], "startnum=") === false){
			$new_request_uri_explode[$element] = $request_uri_explode[$i];
			$element++;
		}
	}
	$request_uri = implode("&", $new_request_uri_explode);	

  if ($start <= 1) {
    $start = 1;
  }
  if ($end >= $totalpage) {
    $end = $totalpage;
  } 
	if ($totalpage > 1) {
?>
		<? 
	if ($page > 1) {
		$back = $page - 1;
		?>
			<a class="small button" href="<?php echo $request_uri;?>&startnum=<? echo '0';?>">
			<? echo "แรกสุด";?>
			</a>
			<a class="small button" href="<?php echo $request_uri;?>&startnum=<? echo ($back*$limit)-$limit;?>">
			<? echo "ก่อนหน้า";?>
			</a>
	<?
	}
	For ($i=$start ; $i<=$end ; $i++) {
 		if ($i == $page+1 ) { 
        
		?>
		 <span class="big button">
			<? echo " $i ";?>
			</span>
	<?
 		} else	 { 
		?>
			<a	class="small button" href="<?php echo $request_uri;?>&startnum=<? echo ($i*$limit)-$limit;?>">
			<? echo " $i ";?>
			</a>
	<?
 			}
	}
    //echo $page.":".$totalpage."<br>";
    $shiftPage = ($page+1);
    if ($shiftPage == $totalpage){

    }else if ($shiftPage < $totalpage) {
 		$next = $page	+1;

		?>
			<a class="small button" href="<?php echo $request_uri;?>&startnum=<? echo ($next*$limit);?>">
			<? echo "ถัดไป";?>
			</a> 
			<a class="small button" href="<?php echo $request_uri;?>&startnum=<? echo ($totalpage*$limit)-$limit;?>">
			<? echo " ท้ายสุด \n";?>
			</a>
    <?
    }
 	?>
<?
}
    else {echo "\n";}
}
?>